﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WMSNet.Common.Services
{
  public class ServiceLocator : IServiceLocator
  {
    private static readonly object _TheLock = new object();
    private IDictionary<Type, Type> _servicesType;
    private readonly IDictionary<Type, object> _instantiatedServices;
    private static IServiceLocator _instance;

    public static IServiceLocator Instance
    {
      get
      {
        lock (ServiceLocator._TheLock)
        {
          if (ServiceLocator._instance == null)
            ServiceLocator._instance = (IServiceLocator) new ServiceLocator();
        }
        return ServiceLocator._instance;
      }
    }

    static ServiceLocator()
    {
    }

    private ServiceLocator()
    {
      this._servicesType = (IDictionary<Type, Type>) new Dictionary<Type, Type>();
      this._instantiatedServices = (IDictionary<Type, object>) new Dictionary<Type, object>();
      this.BuildServiceTypesMap();
    }

    public T GetService<T>()
    {
      if (this._instantiatedServices.ContainsKey(typeof (T)))
        return (T) this._instantiatedServices[typeof (T)];
      try
      {
        T obj = (T) this._servicesType[typeof (T)].GetConstructor(new Type[0]).Invoke((object[]) null);
        this._instantiatedServices.Add(typeof (T), (object) obj);
        return obj;
      }
      catch (KeyNotFoundException ex)
      {
        throw new ApplicationException("The requested service is not registered");
      }
    }

    private void BuildServiceTypesMap()
    {
    }
  }
}
